<gm:page title="My App" authenticate="true">
  
  <h2>Comentários</h2>
  <div style="width:100%" >
  <gm:list id="commentsList" data="${app}/comments" selection="false" sort="data" template="myListTemplate">
      <gm:handleEvent event="save" execute="putUser(event);"/>  
         <!-- <gm:handleEvent event="all" execute="alert(event.type);"/> -->
     <gm:filter ref="atom:title" name="hike" value=""/>
     <gm:sort name="data" ref="atom:updated" reverse="true"/>
  </gm:list>
  </div>
  <p><a id="commentLogin" href="#">Fazer novo comentário</a></p>
  
  <p><button onClick="update();">Limpar</button></p>
  <p><button onClick="updateFilter();">Filter</button></p>
  
  <gm:list id="myListDebug" data="${app}/comments" template="debugTemplate"/>

  <gm:template id="myListTemplate">
    <table class="blue-theme" style="width:100%">
      <tr repeat="true">
        <td style="padding:5px; width:160px"><gm:textarea ref="atom:content"/></td>
        <td style="padding:5px; width:160px"><gm:textarea readonly="true" ref="atom:summary"/></td>
        <td style="padding:5px; width:160px"><gm:date readonly="true" ref="atom:updated"/></td>
        <td style="padding:5px; width:40px"><gm:editButtons/></td>
      </tr>
      <tfoot>
        <tr>
          <td colspan="2" align="center"> <gm:create label="New Comment"/>
          </td>
        </tr>
      </tfoot>
    </table>
  </gm:template>
  
  <gm:template id="debugTemplate">
    <table class="red-theme" style="width:200px">
      <tr repeat="true">
       <td><gm:debug ref="."/></td>
      </tr>
    </table>
  </gm:template>
  
  
  <script>

    if (google.mashups.properties.username) {
      document.getElementById("commentLogin").style.display = "none";    
    } else {
      document.getElementById("commentLogin").href = google.mashups.properties.loginURL;    
    }    
    

    function putUser(event){
      var hidGPath = new GPath("atom:title");
      var authorGPath = new GPath("gd:who/@valueString");
    
      if (event.type == "save") {
        hidGPath.setValue(event.entry, "1");   
        authorGPath.setValue(event.entry, google.mashups.properties.username);
        return;
      }
    }
    
    function updateFilter() {
       var module = google.mashups.getObjectById('commentsList');
       //module.setFilterParam('hike', '');
       module.setFilterParam('hike', "1");    
    }
    
    function update(){
      var list = google.mashups.getObjectById('commentsList');      
      var data = list.getData();
    
      var authorGPath = new GPath("gd:who/@valueString");
      var entry = list.getSelectedEntry();
      if (entry) {
       alert(google.mashups.properties.username);
       authorGPath.setValue(entry, google.mashups.properties.username);
       data.updateEntry(entry);
       alert("atualizaou");
      }   
   }   

    
  </script>
    
    
    
    
</gm:page>